//
//  SearhTableViewController.m
//  Boom
//
//  Created by 云鹏 赵 on 15/3/13.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "SearhTableViewController.h"

@interface SearhTableViewController ()

@end

@implementation SearhTableViewController
- (void)dealloc
{
    [_myTableView release];
    [_albumArray release];
    [_albumArray2 release];
    [_trackArray release];
    [_str release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cyanColor];
    self.title = @"搜索结果";
    // 初始化数组
    self.albumArray = [NSMutableArray array];
    self.trackArray = [NSMutableArray array];
    // 创建tableView
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667 - 64)  style:UITableViewStylePlain];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    [self afnGetData:self.str Page:1];
    [self addFooter];
}
- (void)addFooter
{
    __block SearhTableViewController *vc = self;
    
    [self.myTableView addFooterWithCallback:^{
        
        [vc afnGetData:self.str Page:self.nextPage];
        
    }];
}

// afn获取数据
- (void)afnGetData:(NSString *)string Page:(NSInteger) page
{
    NSString *str = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *url_string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/s/mobile/search?condition=%%20%@&device=iPhone&page=%d&per_page=3&scope=all", str, page];
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        self.nextPage = page + 1;
        
        NSMutableDictionary *allDic = responseObject;
        // 获取专辑数据
        NSMutableDictionary *albumDic = [allDic objectForKey:@"album"];
        NSMutableArray *albumArray = [albumDic objectForKey:@"docs"];
        for (NSMutableDictionary *dic in albumArray) {
            SearchAlbum *searchAlbum = [[SearchAlbum alloc] init];
            [searchAlbum setValuesForKeysWithDictionary:dic];
            [self.albumArray addObject:searchAlbum];
            [searchAlbum release];
        }
        
        // 获取声音数据
        NSMutableDictionary *trackDic = [allDic objectForKey:@"track"];
        NSMutableArray *trackArray = [trackDic objectForKey:@"docs"];
        for (NSMutableDictionary *dic in trackArray) {
            SearchTrack *searchTrack = [[SearchTrack alloc] init];
            [searchTrack setValuesForKeysWithDictionary:dic];
            [self.trackArray addObject:searchTrack];
            [searchTrack release];
        }
//        NSLog(@"album1%@", self.albumArray);
//        NSLog(@"track%@", self.trackArray);
        [self.myTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
        
    }];
    
    
}

// table协议方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* myView = [[[UIView alloc] init] autorelease];
    myView.backgroundColor = [UIColor colorWithRed:244 / 255.0 green:244 / 255.0 blue:244 / 255.0 alpha:1];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];
    titleLabel.textColor=[UIColor blackColor];
    titleLabel.backgroundColor = [UIColor clearColor];
    if (section == 0) {
        titleLabel.text = @"专辑";
    } if (section == 1) {
        titleLabel.text = @"音频";
    }
    [myView addSubview:titleLabel];
    [titleLabel release];
    return myView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"专辑";
    }
    return @"音频";
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 125;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.albumArray.count;
    }
    return self.trackArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        static NSString *cellIdeitifier = @"reuse";
        SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdeitifier];
        if (cell == nil) {
            cell = [[SearchTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdeitifier];
            [cell autorelease];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.searchAlbum = [self.albumArray objectAtIndex:indexPath.row];
        return cell;
    }
    static NSString *cellIdeitifier = @"again";
    SearchTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:cellIdeitifier];
    if (cell == nil) {
        cell = [[SearchTableViewCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdeitifier];
        [cell autorelease];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.searchTrack = [self.trackArray objectAtIndex:indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        SearchAlbum *searchAlbum = [self.albumArray objectAtIndex:indexPath.row];
        [self.albumArray2 removeAllObjects];
        SearchTable2ViewController *searchTVC = [[SearchTable2ViewController alloc] init];
        searchTVC.id = searchAlbum.id;
        [self.navigationController pushViewController:searchTVC animated:YES];
        [searchTVC release];
        
    }
    if (indexPath.section == 1) {
        [[PlayerViewController sharePlayerDataHandel] streamerStop];
        [PlayerViewController sharePlayerDataHandel].searchTrack = [self.trackArray objectAtIndex:indexPath.row];
        [self presentViewController:[PlayerViewController sharePlayerDataHandel] animated:YES completion:^{
            
            [[PlayerViewController sharePlayerDataHandel] buttonPressed];

        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
